package cc.rengu.igas.route.core.realize.payment.impl.upac;

import cc.rengu.igas.channel.upac.biz.impl.UpacCollectionServiceImpl;
import cc.rengu.igas.channel.upac.facade.base.Header;
import cc.rengu.igas.channel.upac.facade.bean.CustomerInfo;
import cc.rengu.igas.channel.upac.facade.request.CollectionSignWithSmsRequest;
import cc.rengu.igas.channel.upac.facade.response.CollectionSignWithSmsResponse;
import cc.rengu.igas.channel.upac.facade.result.Result;
import cc.rengu.igas.route.common.enums.SmartRouteRspEnums;
import cc.rengu.igas.route.core.realize.payment.DstChannelPaymentSignService;
import cc.rengu.igas.route.facade.request.IntelligencePaymentSignRequest;
import cc.rengu.igas.route.facade.request.IntelligenceResultQueryRequest;
import cc.rengu.igas.route.facade.response.IntelligencePaymentSignResponse;
import cc.rengu.igas.route.facade.response.IntelligenceResultQueryResponse;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 全渠道对外收款签约实现
 *
 * @author lizhangtao
 * @version 1.0.0
 * @date 2020-04-30
 */
public class UpacPaymentSignServiceImpl implements DstChannelPaymentSignService {
    private final static String TXN_NUM = "7712000000";
    /**
     * 默认渠道类型 07-互联网
     */
    private final static String DEFAULT_CHANNEL_TYPE = "07";
    /**
     * 通道类型为代收CT  商户选择其开通的产品时取自支付通道信息表T_P_DST_CHANNEL_CFG
     */
    private final static String DST_CHANNEL_TYPE = "CT01";
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public IntelligencePaymentSignResponse paymentSign(IntelligencePaymentSignRequest intelligencePaymentSignRequest) throws Exception {
        IntelligencePaymentSignResponse intelligencePaymentSignResponse = new IntelligencePaymentSignResponse();
        intelligencePaymentSignResponse.setHeader(intelligencePaymentSignRequest.getHeader());
        intelligencePaymentSignResponse.setMchntNo(intelligencePaymentSignRequest.getMchntNo());
        intelligencePaymentSignResponse.setDstChannelId("UPAC");
        intelligencePaymentSignResponse.setVerifyMode("01");
        CollectionSignWithSmsRequest collectionSignWithSmsRequest = new CollectionSignWithSmsRequest();
        collectionSignWithSmsRequest.setProdType(DST_CHANNEL_TYPE);
        collectionSignWithSmsRequest.setChannelType(DEFAULT_CHANNEL_TYPE);
        //银行账号
        collectionSignWithSmsRequest.setPayAcctNo(intelligencePaymentSignRequest.getPayerInfo().getAcctNo());
        collectionSignWithSmsRequest.setOrderId(intelligencePaymentSignRequest.getHeader().getTraceNo());
        collectionSignWithSmsRequest.setOrderTime(intelligencePaymentSignRequest.getHeader().getTransDate() + intelligencePaymentSignRequest.getHeader().getTransTime());
        //客户信息
        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setCustName(intelligencePaymentSignRequest.getPayerInfo().getAcctName());
        //TODO 转换证件类型
        customerInfo.setCertType("01");
        customerInfo.setCertNo(intelligencePaymentSignRequest.getPayerInfo().getCertId());
        customerInfo.setPhoneNo(intelligencePaymentSignRequest.getPayerInfo().getPhoneNo());
        collectionSignWithSmsRequest.setCustomerInfo(customerInfo);
        //设置请求头
        Header header = new Header();
        BeanUtil.beanCopy(intelligencePaymentSignRequest.getHeader(), header);
        header.setTxnNum(TXN_NUM);
        collectionSignWithSmsRequest.setHeader(header);
        //调用通道进行签约(发送短信)
        DubboService dubboService = new DubboServiceImpl();
        Result<CollectionSignWithSmsResponse> signWithSmsRspResult = (Result<CollectionSignWithSmsResponse>)
                dubboService.callDubboInnerMethod(UpacCollectionServiceImpl.class, "collectionSignWithSms", collectionSignWithSmsRequest);
        if (null == signWithSmsRspResult) {
            //签约触发短信的未知可以作为失败重新发起一次触发短信
            throw new BizException(SmartRouteRspEnums.TRANS_FAIL.getRespCode(), SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
        }
        if (!signWithSmsRspResult.isSuccess()) {
            throw new BizException(signWithSmsRspResult.getResult().getRespCode(), signWithSmsRspResult.getResult().getRespDesc());
        }
        intelligencePaymentSignResponse.setRespCode(signWithSmsRspResult.getResult().getRespCode());
        intelligencePaymentSignResponse.setRespDesc(signWithSmsRspResult.getResult().getRespDesc());
        return intelligencePaymentSignResponse;
    }

    @Override
    public IntelligenceResultQueryResponse query(IntelligenceResultQueryRequest resultQueryRequest) throws Exception {
        //签约无查询交易，失败重新签约即可
        throw new BizException(SmartRouteRspEnums.NO_AUTH_ERROR.getRespCode(), SmartRouteRspEnums.NO_AUTH_ERROR.getRespDesc());
    }
}
